import request from "@/utils/request"
import { IAdminPostData, IlistParams } from "./types/admin"
import { IFormData } from "./types/form"

export const getAdmin = (params: IlistParams) => {
  return request({
    method: 'GET',
    url: '/api/setting/admin',
    params
  })
}

export const createAdmin = (data: any) => {
  return request({
    method: 'POST',
    url: '/api/setting/admin',
    data
  })
}

export const updateAdmin = (id: number, data: any) => {
  return request({
    method: 'PUT',
    url: `/api/setting/admin/${id}`,
    data
  })
}

export const deleteAdmin = (id: number) => {
  return request({
    method: 'DELETE',
    url: `/api/setting/admin/${id}`
  })
}

export const updateAdminStatus = (id: number, status: number) => {
  return request({
    method: 'PUT',
    url: `/api/setting/set_status/${id}/${status}`
  })
}

export const getRoles = () => {
  return request<IFormData>({
    method: 'GET',
    url: '/api/setting/admin/create',
  }).then(data => {
    const roles = data.rules.find((item) => item.field === 'roles')
    return roles?.options || []
  })
}

export const getAdminById = (id: number) => {
  return request<IFormData>({
    method: 'GET',
    url: `/api/setting/admin/${id}/edit`,
  }).then(data => {
    const info = data.rules.reduce((acc, cur) => ({ ...acc, [cur.field]: cur.value }), {})
    return info as IAdminPostData
  })
}
